package com.wytest.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wytest.entity.Event;
import com.wytest.entity.Work;
import com.wytest.service.EventService;
import com.wytest.service.WorkService;

@Controller
public class WorkController {

	@Resource
	private WorkService workService;
	@Resource
	private EventService eventService;
	
	@RequestMapping(value="getallwork")
	public ModelAndView getallwork(){
		List<Map<String, Object>> list=workService.getallwork();
		for (int i = 0; i < list.size(); i++) {
			Date date1=(Date)list.get(i).get("again_time");
			Date date2=(Date)list.get(i).get("end_time");
			if(date1!=null&&date2!=null){
				int j = (int) ((date2.getTime() - date1.getTime())/(1000 * 60));
				list.get(i).put("time", j);
			}else{
				list.get(i).put("time", "");
			}
		}
		ModelAndView mav = new ModelAndView();
		mav.addObject("list", list);
		mav.setViewName("work");	
		return mav;
	}
	
	//根据ID查询
			@RequestMapping("getbyid")
			public ModelAndView getbyid(int wid,int term){
			Map<String,Object> map=workService.getbyid(wid);
			List<Event> list=eventService.getAll();
			ModelAndView mav=new ModelAndView();
			mav.setViewName("addwork");
			mav.addObject("term",term);
			mav.addObject("list",list);
			mav.addObject("map",map);
			return mav;
			}
	
	
	@RequestMapping(value="addwork")
	public String add(Work work,int term){
		if(term==0){
			int i=workService.addwork(work);
		if(i>0){
			return "redirect:getallwork.do";
		}
		}
		else{
			workService.updatework(work);
			return "redirect:getallwork.do";	
		}
		return null;
	}
	
		
		//根据ID查询
		@RequestMapping("delwork")
		public String del(int wid){
			workService.delwork(wid);
			return "redirect:getallwork.do";	
		}
}
